package ktt;

import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Tests that Bubonic will stop and continuously fire if target is staying still. Test robot
 * is sitting duck.
 * 
 * @author Kurt Teichman, Philip Johnson
 *
 */
public class TestBubonicFiringAtStationaryTarget extends RobotTestBed {
  /** True if the robot is now hugging the left or right wall. */
  boolean robotOnWall = false;
  /** True if the radar is not moving. */
  boolean radarIsNotMoving = false;
  /** True if the gun is shooting. */
  boolean gunShooting = false;
  /** Stores previous radar heading taken. Initially set to a value outside of the range
   * produced by <code>getRadarHeading()</code> */
  double previousRadarHeading = 999999;
  
  /**
   * Specifies that SittingDuck and Bubonic are to be matched up in this test case.
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,ktt.Bubonic";
  }
  
  /**
   * This test runs for 1 round.
   * @return The number of rounds. 
   */
  @Override
  public int getNumRounds() {
    return 10;
  }
  
  /**
   * After each turn, check to see if Bubonic is on the wall. If so, set the
   * corresponding flag and now check to see if the <code>currentRadarHeading</code>
   * is equal to <code>previousRadarHeading</code>. If this is true, then Bubonic
   * is locked on to a target. Now check to see if the <code>gunHeat</code> is greater
   * than 0, then we know that Bubonic is indeed firing at a stationary target.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override 
  public void onTurnEnded(TurnEndedEvent event) {
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    
    double currentRadarHeading;
    double xPos = robot.getX();
    //Checks to see if the robot is on either the left or right wall
    if (xPos < 40 || xPos > (width - 40)) {
      robotOnWall = true;
    }
    
    if (robotOnWall) {
      currentRadarHeading = Math.toDegrees(robot.getRadarHeading());
      if (previousRadarHeading == currentRadarHeading) {
        radarIsNotMoving = true;
        checkGunHeat(robot.getGunHeat());
      }
      previousRadarHeading = currentRadarHeading;    
    }
  }
  
  /**
   * The method <code>checkGunHeat(gunHeat)</code> sets <code>gunShooting</code>
   * to true if the <code>gunHeat</code> is greater than 0. 
   *
   * @param gunHeat The heat of the gun.
   */
  public void checkGunHeat(double gunHeat) {
    if (gunHeat > 0) {
      gunShooting = true;
    }
  }
  
  
  /**
   * After the battle, check to see that Bubonic's radar as swept the top of the
   * map and the bottom of the map. 
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("If target is stationary, Bubonic is shooting and radar is not moving:",
        radarIsNotMoving && gunShooting);
  }

}
